6 bool visited
[226][226];
13 typedef pair
<int, int> node
;
15 int di
[] = { 0, +1, 0, -1};
16 int dj
[] = {+1, 0, -1, 0};
18 int shoot(const node
&u
, const node
&dad
, const node
&start
){
21 int i
= u
.first
, j
= u
.second
;
24 //printf("Shooting (%d,%d)\n", i, j);
25 bool probable
= false;
26 for (int k
=0; k
<4; ++k
){
30 //printf(" Objetivo: (%d,%d)\n", ni, nj);
32 //printf("r y c son: %d, %d\n", r, c);
34 if (0 <= ni
&& ni
< 3*r
&&
35 0 <= nj
&& nj
< 3*c
&&
38 if (!visited
[ni
][nj
]){
39 cuenta
+= shoot(node(ni
, nj
), u
, start
);
40 }else if (node(ni
, nj
) == start
&& node(ni
, nj
) != dad
){
45 if (probable
&& cuenta
== 1){
54 while ( cin
>> c
>> r
&& r
&& c
){
56 for (int i
=0; i
<3*r
; ++i
) for (int j
=0; j
<3*c
; ++j
){ visited
[i
][j
] = g
[i
][j
] = 0; }
58 for (int i
=0; i
<r
; ++i
){
59 for (int j
=0; j
<c
; ++j
){
63 g
[3*i
][3*j
] = g
[3*i
+1][3*j
+1] = g
[3*i
+2][3*j
+2] = -1;
65 g
[3*i
][3*j
+2] = g
[3*i
+1][3*j
+1] = g
[3*i
+2][3*j
] = -1;
72 for (int i
=0; i
<3*r
; ++i
){
73 for (int j
=0; j
<3*c
; ++j
){
74 printf("%3d", g
[i
][j
]);
81 int longest
= -9999999;
84 for (int i
=0; i
<3*r
; ++i
){
85 for (int j
=0; j
<3*c
; ++j
){
86 if (g
[i
][j
] != -1 && !visited
[i
][j
]){
88 //printf("Shooting (%d,%d)\n", i, j);
91 int dist
= shoot(node(i
, j
), node(-1,-1), node(i
, j
));
92 //cout << "Cycle of length " << dist << " found.\n";
102 for (int i
=0; i
<3*r
; ++i
){
103 for (int j
=0; j
<3*c
; ++j
){
104 printf("%3d", g
[i
][j
]);
111 printf("Maze #%d:\n", mazeNo
++);
113 printf("%d Cycles; the longest has length %d.\n", count
, longest
/3);
115 printf("There are no cycles.\n");